<?php
namespace Client\Controller;
use Think\Controller;
class WoController extends BaseController{
//class WoController extends Controller{
	
	//========================
	protected function _initialize(){
		parent::_initialize();
		header("Content-Type: text/html;charset=utf-8");
		$this->assign('tj',time());
		
		//p($_SESSION);die;
		
		$this->assign('session',$_SESSION);
	}
	
	//我的首页
    public function index(){
    		//抵用券==============
    		$cardnum=M('bb_user_card')->where(array('openid'=>$_SESSION['user']['openid']))->count();
		$this->assign('cardnum',$cardnum);
		//=========
		$this->display();
    }
	//我的信息详情
	public function woinfo(){
		$this->display();
	}
	//修改昵称
	public function editusername(){
		
		$this->display();
	}
	//选择地区
	public function editarea(){
		$area=$this->area_data();
		$area=\bb\Data::channelLevel($area, 0, '-', 'area_id');
		
		$this->wxsdk();
		$this->assign('area',$area);
		$this->display();
	}
	//绑定手机
	public function editphone(){
		
		$this->display();
	}
	
	
	//钱包
	public function qianbao(){
		$this->display();
	}
	
	//电子卡
	public function card(){
		$this->display();
	}
	//电子详情
	public function cardinfo(){
		$this->display();
	}
	//电子充值
	public function cardrecharge(){
		$this->display();
	}
	
	//抵用卷
	public function volume(){
		//抵用券==============
    		$card=M('bb_user_card')->where(array('openid'=>$_SESSION['user']['openid']))->select();
		$this->assign('card',$card);
		$this->assign('newtime',strtotime(time()));
		//=========
		$this->display();
	}
	
	public function wo(){
		$this->display();
	}
	
	//我的推广2维码
	public function fenxiao(){
		
		$options['appid']=C('BB_APPID_client');
		$options['secret']=C('BB_SECRET_client');
		$wx=new \bb\Wechat($options);
		//echo $_SESSION['user']['id'];die;
		$re=$wx->getQRUrl_str('user_'.$_SESSION['user']['id']);
		
		//var_dump($_SESSION['user']);die;
		//echo $_SESSION['user']['id'];die;
		$this->assign('tuigungimg',$re);
		
		$m=M('client_user');
		$map['id'] = $_SESSION['user']['id'];
		
		$data=$m->where($map)->select();
		
		foreach ($data as $k => $v){
			$wxuser=M('weixin_client_user')->field('headimgurl,subscribe_time')->where(array('openid'=>$v['openid']))->find();
			$data[$k]['headimgurl']=$wxuser['headimgurl'];
			$data[$k]['subscribe_time']=$wxuser['subscribe_time'];
			
			$map_fx['path'] = array('like','%,'.$data[$k]['id'].'%');
			$map_fx1 = array();
			$map_fx2 = array();
			$map_fx3 = array();
			$fx=$m->where($map_fx)->select();
			
			foreach($fx as $kv=>$vv){
				$fx_a=explode(',',$vv['path']);
				foreach($fx_a as $ke => $ve){
					if($ve == $data[$k]['id'] ){
						switch ($ke)
						{
						case count($fx_a)-1:
						  $map_fx1[] = $vv['id'];
						  break;  
						case count($fx_a)-2:
						  $map_fx2[] = $vv['id'];
						  break;
						  
						case count($fx_a)-3:
						   $map_fx3[] = $vv['id'];
						  break;
						  
						default:
							
						}
						break;
					}
				}
			}
			$data[$k]['fx1'] = $map_fx1;
			$data[$k]['fx2'] = $map_fx2;
			$data[$k]['fx3'] = $map_fx3;
			$data[$k]['fx_count'] = count($map_fx1)+count($map_fx2)+count($map_fx3);
			
			
			
		}
		
		
		
		$this->assign('xianxiannum',$data[$k]['fx_count']);
		
		$this->display();
	}

	//我的返现
	public function fanxian(){
		$log = M('log_msg');
		
		$arr = $log->where('openid="'.$_SESSION['user']['openid'].'"')->select();
		
		$user =M('client_user');
		foreach($arr as $k=>$v){
			$username= $user->field('username')->where('openid="'.$arr[$k]['openid'].'"')->find();
			$arr[$k]['username'] =$username['username'];
			
		}
		
		$this->assign('arr',$arr);
		$this->display();
	}
	//我的下线会员
	public function xianxia_user(){
		$m=M('client_user');
		$map['id'] = $_SESSION['user']['id'];
		$_GET['type']=intval(I('type'))?intval(I('type')):1;
		$data=$m->where($map)->select();
		
		foreach ($data as $k => $v){
			$wxuser=M('weixin_client_user')->field('headimgurl,subscribe_time')->where(array('openid'=>$v['openid']))->find();
			$data[$k]['headimgurl']=$wxuser['headimgurl'];
			$data[$k]['subscribe_time']=$wxuser['subscribe_time'];
			
			$map_fx['path'] = array('like','%,'.$data[$k]['id'].'%');
			$map_fx1 = array();
			$map_fx2 = array();
			$map_fx3 = array();
			$fx=$m->where($map_fx)->select();
			
			foreach($fx as $kv=>$vv){
				$fx_a=explode(',',$vv['path']);
				foreach($fx_a as $ke => $ve){
					if($ve == $data[$k]['id'] ){
						switch ($ke)
						{
						case count($fx_a)-1:
						  $map_fx1[] = $vv['id'];
						  break;  
						case count($fx_a)-2:
						  $map_fx2[] = $vv['id'];
						  break;
						  
						case count($fx_a)-3:
						   $map_fx3[] = $vv['id'];
						  break;
						  
						default:
							
						}
						break;
					}
				}
			}
			$data[$k]['fx1'] = $map_fx1;
			$data[$k]['fx2'] = $map_fx2;
			$data[$k]['fx3'] = $map_fx3;
			$data[$k]['fx_count'] = count($map_fx1)+count($map_fx2)+count($map_fx3);
			
			
			
		}
		
		if($_GET['type'] == 1){
			$dataxx=array();
			
			foreach($data[0]['fx1'] as $kddd=>$vddd){
				$m=M('client_user');
				$map['id'] = $vddd;
				$dataxx[$kddd]=$m->where($map)->find();
				
				$wxuser=M('weixin_client_user')->field('headimgurl,subscribe_time')->where('openid = "'.$dataxx[$kddd]['openid'].'"')->find();
				
				$dataxx[$kddd]['headimgurl']=$wxuser['headimgurl'];
				$dataxx[$kddd]['subscribe_time']=$wxuser['subscribe_time'];
			}
			
			$this->assign('cache',$dataxx);
			$this->assign('fx_count',count($data[0]['fx1']));
		}elseif($_GET['type'] == 2){
			$dataxx=array();
			
			
			
			foreach($data[0]['fx2'] as $kddd=>$vddd){
				$m=M('client_user');
				$map['id'] = $vddd;
				$dataxx[$kddd]=$m->where($map)->find();
				$wxuser=M('weixin_client_user')->field('headimgurl,subscribe_time')->where(array('openid'=>$dataxx[$kddd]['openid']))->find();
				
				$dataxx[$kddd]['headimgurl']=$wxuser['headimgurl'];
				$dataxx[$kddd]['subscribe_time']=$wxuser['subscribe_time'];
			}
			
			
			
			$this->assign('cache',$dataxx);
			$this->assign('fx_count',count($data[0]['fx2']));
		}elseif($_GET['type'] == 3){
			$dataxx=array();
			
			foreach($data[0]['fx3'] as $kddd=>$vddd){
				$m=M('client_user');
				$map['id'] = $vddd;
				$dataxx[$kddd]=$m->where($map)->find();
				$wxuser=M('weixin_client_user')->field('headimgurl,subscribe_time')->where(array('openid'=>$dataxx[$kddd]['openid']))->find();
				
				$dataxx[$kddd]['headimgurl']=$wxuser['headimgurl'];
				$dataxx[$kddd]['subscribe_time']=$wxuser['subscribe_time'];
			}
			
			$this->assign('cache',$dataxx);
			$this->assign('fx_count',count($data[0]['fx3']));
		}
		
		$this->assign('name',$data[0]['username']);
		$this->assign('type',$_GET['type']);

		
		
		$this->display();
	}
	//投诉建议
	public function toushujianyi(){
		$notice = M('notice_category');
		$list = $notice->where('status = 1')->select();
		$this->assign('list',$list);
		$this->display();
	}
	
	public function dotoushujianyi(){
		$data['type'] = $_POST['type'];
		$data['msg'] = $_POST['content'];
		$data['openid']=$_SESSION['user']['openid'];
		$data['addtime']=time();
		$notice = M('notice');
		$notice->add($data);
		$this->success('投诉成功!',U('Wo/index'));
	}
	
	//余额充值
	public function czyue(){
		$user=M('client_user')->where(array('openid'=>$_SESSION['user']['openid']))->find();
		$this->assign('user',$user);
		$this->display();
	}
	
	//充值
	public function czpay(){
		$czmoeny=I('czmoney');
		if($czmoeny){
			$czlog['openid']=$_SESSION['user']['openid'];
			$czlog['czid']=time();
			$czlog['msg']='会员充值';
			$czlog['money']=$czmoeny;
			$czlog['addtime']=time();
			
			$re=M('usercz_log')->add($czlog);
			if($re){
				$this->wxpay($czmoeny,$czlog['czid'],$_SESSION['user']['openid']);
			}
		}
	}
	//=============================
	public function wxsdk(){
		//分享js 参数------------------------------
		$options['appid']=C('BB_APPID_client');
		$options['secret']=C('BB_SECRET_client');
		$wx=new \bb\Wechat($options);	//new 微信类
		$rejsapi=$wx->getjsapi();
		$this->assign('jsapi',$rejsapi);
		//-----------------------------------
	}
	public function area_data(){
		//缓存地区信息
		//S('privince',null);
		$authname='privince';
		if($rs = S($authname)){
			return $rs;
		}else{
			$area=M('bb_area')->select();
			$expire = 60*60*24;	//每天缓存一次
			S($authname,$area,$expire);
			return $area;
		}
	}
	//====================
	public function jifeninfo(){
		$m=M('log_msg');
		$jf=$m->where(array('openid'=>$_SESSION['user']['openid'],'type'=>1))->select();
		$this->assign('jf',$jf);
		$this->display();
	}
	
	public function yueinfo(){
		
		
	}
	
//==========================================================================
	//微信支付代码
	public function wxpay($money,$czid='',$openid=''){
		header("Content-Type: text/html;charset=utf-8");
		
		//$money=$money;
		$money=0.01;
		$options['appid']=C('BB_APPID_client');
		$options['secret']=C('BB_SECRET_client');
		$options['mch_id']=C('BB_MCHID_client');
		$options['payKey']=C('BB_MCHPAYKEY_client');
		$wx=new \bb\Wechat($options);
		
		$pay['openid']=$_SESSION['openid'];
		$pay['body']='余额充值';
		$pay['orderId']=time();
		$pay['money']=$money;
		$pay['attach']=$czid.'|'.$openid;
		$pay['notify_url']='http://wx.tingzan88.com/Client/notify/wx_cznotify';
		$data=$wx->unifiedOrder($pay['openid'],$pay['body'],$pay['orderId'],$pay['money'],$pay['notify_url'],$pay['attach']);
		foreach($data as $key => $v) {
			$str .= "{$key}={$v}&";
		}
		echo $url="http://wx.tingzan88.com/wxczpay.php?$str".'orderid='.$czid."&money=".$pay['money'];
		header("Location:".$url); 
	}
}